//
//  TSRechargeHeaderView.swift
//  TSProfitProject
//
//  Created by TMS on 2019/11/4.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

protocol TSRechargeHeaderViewDelegate : NSObjectProtocol {
    func didClickRecharge()
}

class TSRechargeHeaderView: UIView {
    
    weak var delegate : TSRechargeHeaderViewDelegate? = nil
    var avatar : UIImageView!
    var nameLabel : UILabel!
    var identifyLabel : UILabel!
    var storeLabel : UILabel!

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = WHITE_COLOR
        configViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func tapGesture(gesture:UITapGestureRecognizer) {
        
        if self.delegate != nil {
            self.delegate?.didClickRecharge()
        }
    }
    
    func refreshHeaderView() {
        storeLabel.text = "中心钱包：￥"+String.init(format: "%.2f", TSLoginManager.shared._usermodel?.zhongxin ?? 0)
    }
    
    func configViews() {
        
        let scale : CGFloat = 375 / 140.0
        let containerView = UIImageView.init(image: UIImage(named: "general_header_bg"))
        self.addSubview(containerView)
        containerView.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(SCREEN_WIDTH / scale)
        }
        
        avatar = UIImageView.init()
        avatar.image = UIImage(named: "general_avatar_default")
        avatar.backgroundColor = WHITE_COLOR
        avatar.layer.cornerRadius = 30
        avatar.layer.masksToBounds = true
        containerView.addSubview(avatar)
        avatar.snp.makeConstraints { (make) in
            make.left.equalTo(16)
            make.top.equalTo(26*T_ScreenScale())
            make.size.equalTo(CGSize(width: 50, height: 50))
        }
        
        nameLabel = UILabel.init()
        nameLabel.text = TSLoginManager.shared._usermodel?.username ?? "暂未设置"
        nameLabel.textColor = WHITE_COLOR
        nameLabel.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        containerView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(avatar.snp.right).offset(13)
            make.top.equalTo(avatar.snp.top).offset(7*T_ScreenScale())
        }
        
        identifyLabel = UILabel.init()
        identifyLabel.text = "ID\(TSLoginManager.shared._usermodel?.user_id ?? 0)"
        identifyLabel.textColor = WHITE_COLOR
        identifyLabel.font = UIFont.systemFont(ofSize: 12)
        containerView.addSubview(identifyLabel)
        identifyLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(10*T_ScreenScale())
        }
        
        storeLabel = UILabel.init()
        storeLabel.text = "中心钱包：￥"+String.init(format: "%.2f", TSLoginManager.shared._usermodel?.zhongxin ?? 0)
        storeLabel.textColor = WHITE_COLOR
        storeLabel.font = UIFont.systemFont(ofSize: 12)
        containerView.addSubview(storeLabel)
        storeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.top.equalTo(identifyLabel.snp.bottom).offset(15*T_ScreenScale())
        }
        
        let btnContainer = UIView.init()
        btnContainer.backgroundColor = WHITE_COLOR
        self.addSubview(btnContainer)
        btnContainer.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(containerView.snp.bottom)
            make.height.equalTo(88)
        }
        
        let interfaceView = UIImageView.init(image: UIImage(named: "account_recharge_btn_bg"))
        interfaceView.isUserInteractionEnabled = true
        interfaceView.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(tapGesture(gesture:))))
        btnContainer.addSubview(interfaceView)
        interfaceView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(40)
            make.size.equalTo(CGSize(width: SCREEN_WIDTH - 2*25, height: 40))
        }
        
        let titleLabel = UILabel.init()
        titleLabel.text = "USDT"
        titleLabel.textColor = WHITE_COLOR
        titleLabel.font = UIFont.systemFont(ofSize: 18)
        interfaceView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
        }
        
        let icon = UIImageView.init(image: UIImage(named: "general_more_white"))
        interfaceView.addSubview(icon)
        icon.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().inset(28)
            make.size.equalTo(CGSize(width: 22, height: 22))
        }
        
//        let commitBtn = UIButton.init(type: .system)
//        commitBtn.setTitle("提交", for: .normal)
//        commitBtn.setTitleColor(WHITE_COLOR, for: .normal)
//        commitBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
//        commitBtn.backgroundColor = BASE_PURPLE_COLOR
//        commitBtn.layer.cornerRadius = 24
//        commitBtn.layer.masksToBounds = true
//        commitBtn.addTarget(self, action: #selector(commitAction), for: .touchUpInside)
//        self.addSubview(commitBtn)
//        commitBtn.snp.makeConstraints { (make) in
//            make.centerX.equalToSuperview()
//            make.top.equalTo(30)
//            make.size.equalTo(CGSize(width: SCREEN_WIDTH*0.682, height: 48))
//        }
    }

}
